package dao;

import java.util.ArrayList;
import java.util.List;

import model.Ap;
import model.Ip;

public class ApDAO extends GenericoDAO<Ap> {

	public ApDAO() {
		super("ips.csv");
	}

	@Override
	public List<Ap> carregar() throws Exception {
		ArrayList<Ap> aps = null;
		aps = new ArrayList<Ap>();
		Ap ap = null;
		String prop = null;
		do {
			prop = buffer.readLine();
			if (prop == null) {
				break;
			}

			String[] apsTexto = prop.split(";");
			String[] camposAps = null;

			for (String s : apsTexto) {
				camposAps = s.split(",");
				ap = new Ap(new Ip(camposAps[0]), camposAps[1], camposAps[2],
						camposAps[3]);
				aps.add(ap);
			}
		} while (true);

		arq.close();

		return aps;
	}

	@Override
	public void salvar(List<Ap> aps) throws Exception {

		for (Ap ap : aps) {
			prop.print(ap.getIp().getIp() + "," + ap.getNome() + ","
					+ ap.getDescricao() + "," + ap.getMarca() + ";\n");
		}
		prop.close();
	}

}
